import 'package:flutter/material.dart';
import 'package:library_base/entities/in_order_page_detail_child_entity.dart';
import 'package:library_base/entities/in_order_page_goods_details_entity.dart';
import 'package:library_base/ext/bool_ext.dart';
import 'package:library_base/ext/double_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/res/widget_generator.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/res/edge.dart';
import 'package:get/get.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

Widget buildBottom(
  ///应收数量
  double totalCount,

  ///实际数量
  double actualCount,
    bool enable,
    {
  void Function()? print,
  void Function()? goodsFinish,
}) {
  return Container(
    color: Colors.white,
    child: Column(
      children: [
        Container(
            height: 166.w,
            decoration: rectTop,
            alignment: Alignment.topRight,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                    child: Container(
                  color: Colors.transparent,
                  child: Stack(
                    children: [
                      Positioned(
                        top: 47.w,
                        left: 24.w,
                        child: RichText(
                          text: TextSpan(
                              text: "${MultiLanguage.actualPick.tr}: ",
                              style: TextStyles.textNormal28,
                              children: [
                                TextSpan(
                                    text: '$actualCount',
                                    style: TextStyles.primaryColor28),
                                TextSpan(
                                    text: '/$totalCount',
                                    style: TextStyles.textNormal28),
                              ]),
                        ),
                      ),
                    ],
                  ),
                )),
                Visibility(
                  visible: false,
                  child: GradientButton(
                    ThemeUtil.getTheme(),
                    decoration: BoxDecoration(
                        borderRadius: const BorderRadius.all(Radius.circular(20)),
                        border: Border.fromBorderSide(BorderSide(
                            color: ThemeUtil.getTheme().sixthTextColor())),
                        color: ThemeUtil.getTheme().colorScheme.secondary),
                    height: 77.w,
                    width: 184.w,
                    normalColor: ThemeUtil.getTheme().colorScheme.secondary,
                    normalTextColor: ThemeUtil.getTheme().colorScheme.secondary,
                    textStyle: enable? normalTextColor28 : secondaryTextColor28,
                    margin: EdgeInsets.only(right: 16.w, top: 21.w),
                    text: MultiLanguage.print.tr,
                    onPressed: enable ? print:null,
                  ),
                ),
                GradientButton(
                  ThemeUtil.getTheme(),
                  height: 77.w,
                  width: 184.w,
                  margin: EdgeInsets.only(right: 24.w, top: 21.w),
                  text: MultiLanguage.goodsFinish.tr,
                  onPressed: enable ? goodsFinish:null,
                ),
              ],
            )),
      ],
    ),
  );
}

Widget buildItem(
  InOrderPageDetailChildEntity taskEntity,
  int position, {
  Function(InOrderPageDetailChildEntity taskEntity, int position)?
      singleHeadEntry,
      Function(InOrderPageDetailChildEntity taskEntity, int position)?
      noSingleHeadEntry,
}) {
  double totalWeight = 0.0;
  double count = 0.0;

  if(taskEntity.singleEntry){
    for (int i = 0; i < taskEntity.goodsDetails.length; i++) {
      InOrderPageGoodsDetailsEntity detailsEntity = taskEntity.goodsDetails[i];
      totalWeight = NumUtil.add(totalWeight, detailsEntity.stockInWeight);
      if (detailsEntity.stockInWeight > 0) {
        count++;
      }
    }
  }else{
    totalWeight = taskEntity.stockInWeight;
    count = taskEntity.stockInCount;
  }

  List<Widget> widgets = [];

  widgets.add(gapH24);

  ///第一行内容
  widgets.add(Container(
    padding: EdgeInsets.symmetric(horizontal: 24.w),
    child: Row(
      children: [
        Text("${MultiLanguage.productName.tr} : ", style: normalTextColor28),
        Expanded(child: Text(taskEntity.goodsName, style: normalTextColor28)),
      ],
    ),
  ));

  widgets.add(gapH18);

  widgets.add(Container(
    padding: EdgeInsets.symmetric(horizontal: 24.w),
    child: Row(
      children: [
        Expanded(
          child: Row(
            children: [
              Text("${MultiLanguage.commodityQuantity.tr} : ",
                  style: normalTextColor28),
              Expanded(
                  child: Text("${taskEntity.purchaseCount}",
                      style: normalTextColor28)),
            ],
          ),
        ),
        Expanded(
          child: Row(
            children: [
              Text("${MultiLanguage.goodsQuantity.tr} : ", style: count>0.0?normalTextColor28:eighthTextColor28),
              Expanded(child: Text("$count", style: count>0.0?normalTextColor28:eighthTextColor28)),
            ],
          ),
        ),
      ],
    ),
  ));

  widgets.add(gapH18);

  widgets.add(Container(
    padding: EdgeInsets.symmetric(horizontal: 24.w),
    child: Row(
      children: [
        Expanded(
          child: Row(
            children: [
              Text("${MultiLanguage.orderWeight.tr}${SPUtil.isJin.unitStrZhWithBracket()} : ", style: normalTextColor28),
              Expanded(
                  child: Text(taskEntity.purchaseWeight.remoteUnitTransitionLocalUnitStr(taskEntity.unit),
                      style: normalTextColor28)),
            ],
          ),
        ),
        Expanded(
          child: Row(
            children: [
              Text("${MultiLanguage.goodsWeight.tr}${SPUtil.isJin.unitStrZhWithBracket()} : ", style: totalWeight>0.0?normalTextColor28:eighthTextColor28),
              Expanded(child: Text(totalWeight.remoteUnitTransitionLocalUnitStr(taskEntity.unit), style: totalWeight>0.0?normalTextColor28:eighthTextColor28)),
            ],
          ),
        ),
      ],
    ),
  ));
  widgets.add(Visibility(
    visible: taskEntity.singleEntry,
    child: Container(
      alignment: Alignment.center,
      child: GradientButton(
        ThemeUtil.getTheme(),
        decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(20)),
            border: Border.fromBorderSide(BorderSide(
                color: ThemeUtil.getTheme().fourthTextColor())),
            color: ThemeUtil.getTheme().colorScheme.secondary),
        margin: EdgeInsets.only(right: 16.w, top: 30.w,left: 24.w,bottom: 6.w),
        padding: EdgeInsets.symmetric(horizontal: 50.w, vertical: 15.w),
        text: MultiLanguage.singleHeadEntry.tr,
        normalColor: ThemeUtil.getTheme().colorScheme.secondary,
        normalTextColor: ThemeUtil.getTheme().fourthTextColor(),
        textStyle: fourthTextColor26,
        onPressed: () {
          singleHeadEntry?.call(taskEntity, position);
        },
      ),
    ),
  ));
  widgets.add(gapH24);
  return GestureDetector(
    onTap: () {
      if(!taskEntity.singleEntry){
        noSingleHeadEntry?.call(taskEntity, position);
      }
    },
    child: roundWidget(
      margin: edgeLR24,
      decoration: rs8,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Expanded(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: widgets,
            ),
          ),
        ],
      ),
    ),
  );
}
